package org.particlethink.mapper.impl;

import org.particlethink.config.RequestConfig;
import org.particlethink.context.Context;
import org.particlethink.job.Result;
import org.particlethink.job.Runnable;
import org.particlethink.mapper.SupplierMapper;
import org.particlethink.model.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
//@ConditionalOnMissingBean(DevSupplierMapperImpl.class)
@ConditionalOnExpression("#{!'dev'.equals('${spring.profiles.active}')}")
public class ProSupplierMapperImpl implements SupplierMapper, Runnable<List<Supplier>> {

    @Autowired
    private RequestConfig requestConfig;

    @Override
    public List<Supplier> selectByIds(String token, List<Integer> supplierIDs) {

        try {
            List<Supplier> request = ProSupplierRequestHelper.request(requestConfig.getUrl(), null, null, 1, 10000, token);
            return request.stream().filter(supplier -> {
                if (supplierIDs == null || supplierIDs.isEmpty()) {
                    return true;
                }
                return supplierIDs.contains(supplier.getId());
            }).collect(Collectors.toList());
        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Supplier selectByPrimaryKey(String token, Integer supplierID) {
        try {
            List<Supplier> request = ProSupplierRequestHelper.request(requestConfig.getUrl(), null, null, 1, 10000,token);
            return request.stream().filter(supplier -> Objects.equals(supplier.getId(), supplierID)).findFirst().get();
        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Supplier selectById(String token, Integer supplierId) {
        return this.selectByPrimaryKey(token, supplierId);
    }

    @Override
    public List<Supplier> SelectByName(String token, String name) {
        try {
            return ProSupplierRequestHelper.request(requestConfig.getUrl(), null, name, 1, 10000, token);
        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Supplier> selectByNos(String token, List<String> supplierCodes) {
        try {
            List<Supplier> request = ProSupplierRequestHelper.request(requestConfig.getUrl(), null, null, 1, 10000, token);
            return request.stream().filter(supplier -> {
                if (supplierCodes == null || supplierCodes.isEmpty()) {
                    return false;
                }
                return supplierCodes.contains(supplier.getNo());
            }).collect(Collectors.toList());
        } catch (URISyntaxException | IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Result<List<Supplier>> run() {
        return null;
    }
}
